package com.kingsoft.dc.khaos.plugin.core.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kingsoft.dc.khaos.plugin.core.exception.ParamParseException;
import com.ksyun.kbdp.dts.common.utils.Props;

import java.util.*;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;

/**
 * Created by jing on 19/6/22.
 */
public class DtsArgParserUtils {
//    private static Logger logger = LoggerFactory.getLogger(DtsArgParserUtils.class);

    final public static String OPERATOR_NAME = "operator_name";
    final public static String OPERATOR_TYPE = "type";
    final public static String CLAZZ = "clazz";
    final public static String GRAPH = "graph";
    final public static String WIDGETS = "widgets";
    final public static String STRATEGY = "strategy";
    final public static String CONFIG = "config";
    final public static String EXTENDER = "extender";
    final public static String EXTRACT_FIELDS = "extract_fields";
    final public static String COLS = "cols";
    final public static String COLNAME = "colName";
    final public static String HBASE = "hbase";
    final public static String VIRTUAL = "virtual";
    final public static String PLUGIN_PARAMS = "plugin_param";
    final public static String PARAMS = "params";
    final public static String META = "meta";
    final public static String AUTH = "auth";
    final public static String DS_ID = "ds_id";
    final public static String DS_TYPE = "ds_type";
    final public static String DS_NAME = "ds_name";
    final public static String SQL = "sql";
    final public static String KYLIN = "kylin";

    public static Map<String, String> fieldMapping = new HashMap<>();

    static {
        fieldMapping = new HashMap<>();
        fieldMapping.put("colName", "field");
        fieldMapping.put("colType", "data_type");
    }

    /**
     * 每种插件代码片段配置提取key
     */
    public static Map<String, String[]> commandMappingForPluginParam = new HashMap<>();

    static {
        commandMappingForPluginParam.put("mpp-sql", new String[]{STRATEGY, CONFIG, SQL});
    }

    public static Map<String, String> commandKeyMapping = new HashMap<>();

    static {
        commandKeyMapping.put("mpp-sql", SQL);
    }

    /**
     * 转换前端构建的数据同步和数据加工的DAG参数，根据算子类型初始化实现类的clazz
     *
     * @param syncStr
     * @return
     */
    public static Map parseSyncStr(String syncStr) {
        Map<String, Object> syncMap = JSON.parseObject(syncStr, Map.class);
        Map<String, String> newSyncMap = parseSyncMap(syncMap);
        return newSyncMap;
    }

    /**
     * 根据算子类型和算子名称，获取算子实现类
     *
     * @param userConfig   算子类型、算子名称和算子具体实现类的配置映射关系
     * @param type
     * @param operatorName
     * @return
     */
    public static String getOperatorClazz(UserConfig userConfig, String type, String operatorName) {
        String operatorClazz = "";
        try {
            operatorClazz = userConfig.getValueByKey(type, operatorName, "clazz").toString();
        } catch (Exception e) {
            throw new ParamParseException("can't found operator clazz for type:" + type + ",operatorName:" + operatorName);
        }

        return operatorClazz;
    }

    /**
     * 根据算子类型，获取算子base实现类
     *
     * @param userConfig 算子类型和算子具体实现类的配置映射关系
     * @param type       算子类型
     * @return
     */
    public static String getBaseClazz(UserConfig userConfig, String type) {
        String clazz = "";
        try {
            clazz = userConfig.getValueByKey(type, "type", "clazz").toString();
        } catch (Exception e) {
            throw new ParamParseException("can't found base clazz for type:" + type);
        }
        return clazz;
    }

    /**
     * 从map中逐层提取keys的值
     *
     * @param map
     * @param keys
     * @return
     */
    public static Object extractValue(Map<String, Object> map, String... keys) {
        List<String> keyList = Arrays.asList(keys);
        List<String> middleKeys = keyList.subList(0, keyList.size() - 1);
        String leafKey = keyList.get(keyList.size() - 1);
        for (String key : middleKeys) {
            map = JSON.parseObject(map.get(key).toString(), Map.class);
        }
        return map.get(leafKey);
    }


    /**
     * 映射字段名,colName->field_name,colType->field_type
     *
     * @param fields
     * @return
     */
    public static List<Map<String, Object>> parseField(Object fields) {
        List<Map> exFilelds = JSONArray.parseArray(fields.toString(), Map.class);
        List<Map<String, Object>> parsedFields = new ArrayList<Map<String, Object>>();
        for (Map<String, Object> field : exFilelds) {
            Map<String, Object> newMap = new HashMap<>();
            for (Map.Entry<String, Object> entry : field.entrySet()) {
                if (fieldMapping.containsKey(entry.getKey())) {
                    newMap.put(fieldMapping.get(entry.getKey()), entry.getValue());
                } else {
                    newMap.put(entry.getKey(), entry.getValue());
                }
            }
            parsedFields.add(newMap);
        }
        return parsedFields;
    }

    /**
     * 转换hbase字段定义
     *
     * @param fields
     * @return
     */
    public static List<Map<String, Object>> parseHbaseField(Object fields) {
        List<Map> colFamilies = JSONArray.parseArray(fields.toString(), Map.class);
        List<Map<String, Object>> parsedFields = new ArrayList<Map<String, Object>>();
        for (Map<String, Object> colFamily : colFamilies) {
            if (colFamily.containsKey(COLNAME)) {
                String colFamilyName = colFamily.get(COLNAME).toString();
                //有嵌套
                if (colFamily.containsKey(COLS)) {
                    List<Map> cols = JSONObject.parseArray(colFamily.get(COLS).toString(), Map.class);
                    for (Map<String, Object> col : cols) {
                        Map<String, Object> newMap = new HashMap<>();
                        for (Map.Entry<String, Object> entry : col.entrySet()) {
                            String colName = entry.getKey();
                            Object colValue = entry.getValue();
                            if (fieldMapping.containsKey(colName)) {
                                colName = fieldMapping.get(colName);
                                if (entry.getKey().equalsIgnoreCase(COLNAME)) {
                                    colValue = colFamilyName + ":" + colValue.toString();
                                }
                            }
                            newMap.put(colName, colValue);
                        }
                        parsedFields.add(newMap);
                    }
                }
                //没有嵌套，直接替换字段名
                else {
                    parsedFields = parseField(fields);
                    break;
                }
            }
        }
        return parsedFields;
    }

    /**
     * 转换前端构建的DAG参数，根据算子类型初始化实现类的clazz
     *
     * @param syncMap 前端参数MAP
     * @return
     */
    public static Map parseSyncMap(Map<String, Object> syncMap) throws ParamParseException {
        UserConfig userConfig = new UserConfig();
        Map<String, Object> newSyncMap = syncMap;
        if (newSyncMap.containsKey(GRAPH)) {
            Map<String, Object> graph = JSON.parseObject(newSyncMap.get(GRAPH).toString(), Map.class);
            List<Object> widgets = JSONObject.parseArray(graph.get(WIDGETS).toString());
            List<Object> newWidgets = new ArrayList<>();
            for (Object widget : widgets) {
                Map widgetMap = parseParams(userConfig, widget);
                newWidgets.add(widgetMap);
            }
            graph.put(WIDGETS, newWidgets);
            newSyncMap.put(GRAPH, graph);
        } else if (newSyncMap.containsKey(PLUGIN_PARAMS)) {
            Map widgetMap = parseParams(userConfig, newSyncMap.get(PLUGIN_PARAMS));
            newSyncMap.put(PLUGIN_PARAMS, widgetMap);
        }
        return newSyncMap;
    }

    /**
     * 转换每个组件配置参数，初始化实现类的clazz
     *
     * @param userConfig
     * @param widget
     * @return
     */
    public static Map parseParams(UserConfig userConfig, Object widget) throws ParamParseException {
        Map widgetMap = JSON.parseObject(widget.toString(), Map.class);
        String type = widgetMap.get(OPERATOR_TYPE).toString();
        if (type.equalsIgnoreCase(VIRTUAL)) {
            return widgetMap;
        }
        if (type.equalsIgnoreCase(KYLIN)) {
            return widgetMap;
        }
        String operatorName = widgetMap.get(OPERATOR_NAME).toString();
        widgetMap.put(CLAZZ, getBaseClazz(userConfig, type));
        widgetMap.put("engine", "SPARK");
        widgetMap.put("clazz_type", type);
        Map stratepyMap = JSON.parseObject(widgetMap.get(STRATEGY).toString(), Map.class);
        stratepyMap.put(CLAZZ, getOperatorClazz(userConfig, type, operatorName));
        Map stratepyConfig = JSON.parseObject(stratepyMap.get(CONFIG).toString(), Map.class);
        if (stratepyConfig.containsKey(EXTRACT_FIELDS)) {
            List<Map<String, Object>> newFields = new ArrayList<>();
            if (operatorName.equalsIgnoreCase(HBASE) && type.equalsIgnoreCase("source")) {
                newFields = parseHbaseField(stratepyConfig.get(EXTRACT_FIELDS));
            } else {
                newFields = parseField(stratepyConfig.get(EXTRACT_FIELDS));
            }
            stratepyConfig.put(EXTRACT_FIELDS, newFields);
        }
        if (stratepyConfig.containsKey(EXTENDER)) {
            Map<String, Object> extenderMap = JSON.parseObject(extractValue(stratepyMap, CONFIG, EXTENDER).toString(), Map.class);
            for (String extenderKey : extenderMap.keySet()) {
                Map extenderValue = JSON.parseObject(extenderMap.get(extenderKey).toString(), Map.class);
                Object clazz = userConfig.getValueByKey(type, operatorName, EXTENDER, extenderKey, CLAZZ);
                if (clazz != null) {
                    extenderValue.put(CLAZZ, clazz);
                }
                extenderMap.put(extenderKey, extenderValue);
            }
            stratepyConfig.put(EXTENDER, extenderMap);
        }
        encodeCode(stratepyConfig, operatorName, type);
        stratepyMap.put(CONFIG, stratepyConfig);
        widgetMap.put(STRATEGY, stratepyMap);
        return widgetMap;
    }


    /**
     * 从同步参数配置中提取数据源ID
     *
     * @param paramStr eg:{"strategy":{"config":{"extender":{"auth":{"params":{"ds_type":"mpp","ds_id":179},"clazz":""},"meta":{"params":{"ds_type":"mpp","ds_id":179},"clazz":""}},"advanced_options":{},"sql":"insert into di_mpp_db.src_table (select id,name from di_mpp_db.del_table)"},"clazz":""}
     * @return
     */
    public static DsInfo extractDsInfo(String paramStr) {
        int dsId = -1;
        String dsType = "-";
        String dsName = "-";
        DsInfo dsInfo = new DsInfo();
        Map<String, Object> paramMap = JSON.parseObject(paramStr, Map.class);
        if (JsonUtils.containsKey(paramMap, STRATEGY, CONFIG, EXTENDER, AUTH, PARAMS, DS_ID)) {
            String dsIdStr = JsonUtils.extractStringValue(paramMap, STRATEGY, CONFIG, EXTENDER, AUTH, PARAMS, DS_ID);
            if (dsIdStr != null && !dsIdStr.isEmpty()) {
                dsId = Integer.parseInt(dsIdStr);
                if (JsonUtils.containsKey(paramMap, STRATEGY, CONFIG, EXTENDER, AUTH, PARAMS, DS_TYPE)) {
                    dsType = JsonUtils.extractStringValue(paramMap, STRATEGY, CONFIG, EXTENDER, AUTH, PARAMS, DS_TYPE);
                }
                if (JsonUtils.containsKey(paramMap, STRATEGY, CONFIG, EXTENDER, AUTH, PARAMS, DS_NAME)) {
                    dsName = JsonUtils.extractStringValue(paramMap, STRATEGY, CONFIG, EXTENDER, AUTH, PARAMS, DS_NAME);
                }
                dsInfo.setDsId(dsId);
                dsInfo.setDsType(dsType);
                dsInfo.setDsName(dsName);
                return dsInfo;
            } else {
                return null;
            }

        } else {
            return null;
        }
    }

    /**
     * 提取代码片段
     *
     * @param paramMap
     * @return
     */
    public static String extractCode(Map paramMap) {
        String code = null;
        if (paramMap.containsKey(PLUGIN_PARAMS)) {
            Map pluginParams = JsonUtils.extractMapValue(paramMap, PLUGIN_PARAMS);
            if (pluginParams.containsKey(OPERATOR_NAME) && pluginParams.containsKey(OPERATOR_TYPE)) {
                String opName = pluginParams.get(OPERATOR_NAME) + "-" + pluginParams.get(OPERATOR_TYPE);
                if (commandMappingForPluginParam.containsKey(opName)) {
                    if (JsonUtils.containsKey(pluginParams, commandMappingForPluginParam.get(opName))) {
                        code = JsonUtils.extractStringValue(pluginParams, commandMappingForPluginParam.get(opName));
                    }
                }
            }

        }
        return code;
    }

    /**
     * 对代码片段进行编码
     *
     * @param configMap
     * @return
     */
    public static void encodeCode(Map configMap, String operatorName, String type) {
        String opType = operatorName + "-" + type;
        if (commandKeyMapping.containsKey(opType)) {
            String codeKey = commandKeyMapping.get(opType);
            if (configMap.containsKey(codeKey)) {
                configMap.put(codeKey, Base64CodeUtils.encode(configMap.get(codeKey).toString()));
            }
        }
    }


    public static void main(String[] args) {
//        String syncStr = "{\"graph\":{\"edges\":[{\"destination\":{\"node\":\"41\",\"seq\":\"0\"},\"source\":{\"node\":\"40\",\"seq\":\"0\"}},{\"destination\":{\"node\":\"42\",\"seq\":\"0\"},\"source\":{\"node\":\"41\",\"seq\":\"0\"}}],\"widgets\":[{\"operator_name\":\"hbase\",\"name\":\"source_1564664549507\",\"id\":\"40\",\"type\":\"source\",\"strategy\":{\"clazz\":\"\",\"config\":{\"filter\":\"select 1 from \",\"extender\":{\"auth\":{\"params\":{\"ds_name\":\"hbase_no_kerberos\",\"user_id\":\"1\",\"project_id\":8,\"ds_type\":\"hbase\",\"ds_id\":190},\"clazz\":\"\"},\"meta\":{\"params\":{\"ds_name\":\"hbase_no_kerberos\",\"user_id\":\"1\",\"project_id\":8,\"ds_type\":\"hbase\",\"ds_id\":190},\"clazz\":\"\"}},\"db_name\":\"namespace_1\",\"table_name\":\"hbase_t1\",\"advanced_options\":{\"data_version\":{\"on_off\":false,\"min_timestamp\":\"\",\"max_timestamp\":\"\",\"max_versions\":\"1\",\"timestamp\":\"\"},\"rowkey_range\":{\"row_start\":\"\",\"on_off\":false,\"row_end\":\"\"}},\"extract_fields\":[{\"colName\":\"test:a1\",\"colType\":\"STRING\",\"length\":\"50\",\"colCode\":\"col-7fd8907ae0504732afa574ef6c9045ff\",\"techLogicTableId\":988,\"id\":4041,\"position\":0},{\"colName\":\"test:a2\",\"colType\":\"NUMBER\",\"length\":\"9\",\"colCode\":\"col-e146cbd24ba94850a4154b75633223fc\",\"techLogicTableId\":988,\"id\":4042,\"position\":0},{\"colName\":\"test:a3\",\"colType\":\"DECIMAL\",\"length\":\"15,2\",\"colCode\":\"col-203a1ac5c735411f99f72f4b13937dee\",\"techLogicTableId\":988,\"id\":4043,\"position\":0},{\"colName\":\"test:a4\",\"colType\":\"DATE\",\"colCode\":\"col-ca7420dfd8724a009918ca376a7fc332\",\"techLogicTableId\":988,\"id\":4044,\"position\":0},{\"colName\":\"test:a5\",\"colType\":\"DATETIME\",\"colCode\":\"col-0406fb56b5ba4fe385e21203f6639555\",\"techLogicTableId\":988,\"id\":4045,\"position\":0}]}}},{\"operator_name\":\"map\",\"name\":\"map_1564664564751\",\"id\":\"41\",\"type\":\"transform\",\"strategy\":{\"clazz\":\"\",\"config\":{\"filter\":\"\",\"advanced_options\":{\"partition\":{\"on_off\":false,\"nums\":\"\"},\"cache_option\":{\"on_off\":false,\"option\":\"cache\"}},\"extract_fields\":[{\"field\":\"test:a1\",\"data_type\":\"STRING\",\"length\":\"50\",\"tag\":\"source_column\",\"option\":\"\"},{\"field\":\"test:a2\",\"data_type\":\"NUMBER\",\"length\":\"9\",\"tag\":\"source_column\",\"option\":\"\"},{\"field\":\"test:a3\",\"data_type\":\"DECIMAL\",\"length\":\"15,2\",\"tag\":\"source_column\",\"option\":\"\"},{\"field\":\"test:a4\",\"data_type\":\"DATE\",\"tag\":\"source_column\",\"option\":\"\"},{\"field\":\"test:a5\",\"data_type\":\"DATETIME\",\"tag\":\"source_column\",\"option\":\"\"}]}}},{\"operator_name\":\"oracle\",\"name\":\"target_1564664612271\",\"id\":\"42\",\"type\":\"sink\",\"strategy\":{\"clazz\":\"\",\"config\":{\"extender\":{\"auth\":{\"params\":{\"ds_name\":\"ksyun_test\",\"user_id\":\"1\",\"project_id\":8,\"ds_type\":\"oracle\",\"ds_id\":91},\"clazz\":\"\"},\"meta\":{\"params\":{\"ds_name\":\"ksyun_test\",\"user_id\":\"1\",\"project_id\":8,\"ds_type\":\"oracle\",\"ds_id\":91},\"clazz\":\"\"}},\"db_name\":\"ksyun\",\"write_option\":\"append\",\"table_name\":\"mysql_mzq_t2\",\"advanced_options\":{},\"extract_fields\":[{\"field_props\":{\"key_type\":\"\",\"not_null\":\"false\",\"is_partition\":false,\"default_value\":\"\"},\"field\":\"a1\",\"from_field\":\"test:a1\",\"data_type\":\"STRING\",\"length\":\"50\"},{\"field_props\":{\"key_type\":\"\",\"not_null\":\"false\",\"is_partition\":false,\"default_value\":\"\"},\"field\":\"a2\",\"from_field\":\"test:a2\",\"data_type\":\"NUMBER\",\"length\":\"9\"},{\"field_props\":{\"key_type\":\"\",\"not_null\":\"false\",\"is_partition\":false,\"default_value\":\"\"},\"field\":\"a3\",\"from_field\":\"test:a3\",\"data_type\":\"DECIMAL\",\"length\":\"15,2\"},{\"field_props\":{\"key_type\":\"\",\"not_null\":\"false\",\"is_partition\":false,\"default_value\":\"\"},\"field\":\"a4\",\"from_field\":\"test:a4\",\"data_type\":\"DATE\"},{\"field_props\":{\"key_type\":\"\",\"not_null\":\"false\",\"is_partition\":false,\"default_value\":\"\"},\"field\":\"a5\",\"from_field\":\"test:a5\",\"data_type\":\"DATETIME\"}]}}}]}}";
//        String syncStr = "{\"edges\":[{\"destination\":{\"node\":\"1908081648122\",\"seq\":\"0\"},\"source\":{\"node\":\"1908081646036\",\"seq\":\"0\"}},{\"destination\":{\"node\":\"955737\",\"seq\":\"0\"},\"source\":{\"node\":\"1908081644133\",\"seq\":\"0\"}},{\"destination\":{\"node\":\"1908081646036\",\"seq\":\"0\"},\"source\":{\"node\":\"955737\",\"seq\":\"0\"}},{\"destination\":{\"node\":\"5955737\",\"seq\":\"0\"},\"source\":{\"node\":\"955737\",\"seq\":\"0\"}}],\"widgets\":[{\"run_env\":\"test\",\"operator_name\":\"techCheck\",\"name\":\"source_1565253881102\",\"id\":\"955737\",\"type\":\"transform\",\"strategy\":{\"clazz\":\"\",\"config\":{\"extender\":{\"check\":{\"params\":{\"ds_name\":\"di_mysql_test\",\"rule_ids\":[1,33],\"project_id\":149,\"ds_type\":\"mysql\",\"ds_id\":100},\"clazz\":\"\"}},\"db_name\":\"di_mysql_check\",\"table_name\":\"di_tech_check\",\"extract_fields\":[{\"field\":\"di_string\",\"data_type\":\"STRING\",\"length\":\"10\",\"option\":{\"tech_check\":{\"rule_options\":[{\"rule_id\":1,\"__key__\":\"01DHR6FNACS9R1YVT6P4VD6QZH\",\"rule_type\":\"MESSY_CODE\",\"rule_name\":\"乱码检查\",\"__index__\":0}],\"on_off\":true}}},{\"field\":\"di_int\",\"data_type\":\"NUMBER\",\"length\":\"10\",\"option\":{\"tech_check\":{\"rule_options\":[{\"rule_id\":33,\"__key__\":\"01DHR6EVSYW7C50VAH0P9E6YYS\",\"rule_type\":\"CONTENT_LENGTH\",\"rule_name\":\"内容长度检查\",\"__index__\":0}],\"on_off\":true}}},{\"field\":\"di_decimal\",\"data_type\":\"DECIMAL\",\"length\":\"10,2\",\"option\":{\"tech_check\":{\"rule_options\":[],\"on_off\":false}}},{\"field\":\"di_date\",\"data_type\":\"DATE\",\"length\":\"10\",\"option\":{\"tech_check\":{\"rule_options\":[],\"on_off\":false}}},{\"field\":\"di_time\",\"data_type\":\"TIME\",\"length\":\"8\",\"option\":{\"tech_check\":{\"rule_options\":[],\"on_off\":false}}},{\"field\":\"di_datetime\",\"data_type\":\"DATETIME\",\"length\":\"19\",\"option\":{\"tech_check\":{\"rule_options\":[],\"on_off\":false}}}]}}},{\"operator_name\":\"checkSink\",\"name\":\"检核上报\",\"id\":\"5955737\",\"type\":\"virtual\"},{\"operator_name\":\"mysql\",\"name\":\"source_1565253881102\",\"id\":\"1908081644133\",\"type\":\"source\",\"strategy\":{\"clazz\":\"\",\"config\":{\"filter\":\"\",\"extender\":{\"auth\":{\"params\":{\"ds_name\":\"di_mysql_test\",\"user_id\":\"1\",\"project_id\":149,\"ds_type\":\"mysql\",\"ds_id\":100},\"clazz\":\"\"},\"meta\":{\"params\":{\"ds_name\":\"di_mysql_test\",\"user_id\":\"1\",\"project_id\":149,\"ds_type\":\"mysql\",\"ds_id\":100},\"clazz\":\"\"}},\"db_name\":\"di_mysql_check\",\"table_name\":\"di_tech_check\",\"advanced_options\":{\"split_mode\":{\"split_field\":\"id\",\"on_off\":false,\"split_nums\":\"1\"}},\"extract_fields\":[{\"colName\":\"di_string\",\"colType\":\"STRING\",\"length\":\"10\",\"colCode\":\"col-b26b748935ff4805a1e64e0408e7ff0c\",\"techLogicTableId\":1592,\"id\":6214,\"position\":0},{\"colName\":\"di_int\",\"colType\":\"NUMBER\",\"length\":\"10\",\"colCode\":\"col-9964ec91585e4eeb853cda6b3604b8b3\",\"techLogicTableId\":1592,\"id\":6215,\"position\":0},{\"colName\":\"di_decimal\",\"colType\":\"DECIMAL\",\"length\":\"10,2\",\"colCode\":\"col-ad28868c43124883923499abed0c5bf2\",\"techLogicTableId\":1592,\"id\":6216,\"position\":0},{\"colName\":\"di_date\",\"colType\":\"DATE\",\"length\":\"10\",\"colCode\":\"col-f80e87015f65417281121150d3d44d54\",\"techLogicTableId\":1592,\"id\":6217,\"position\":0},{\"colName\":\"di_time\",\"colType\":\"TIME\",\"length\":\"8\",\"colCode\":\"col-ed3ee45bb2ce489a88a06fc5fa5a075a\",\"techLogicTableId\":1592,\"id\":6218,\"position\":0},{\"colName\":\"di_datetime\",\"colType\":\"DATETIME\",\"length\":\"19\",\"colCode\":\"col-57e8f2f3ae094a30a7c325228fab20d1\",\"techLogicTableId\":1592,\"id\":6219,\"position\":0}]}}},{\"operator_name\":\"map\",\"name\":\"map_1565253999011\",\"id\":\"1908081646036\",\"type\":\"transform\",\"strategy\":{\"clazz\":\"\",\"config\":{\"filter\":\"\",\"advanced_options\":{\"partition\":{\"on_off\":false,\"nums\":\"\"},\"cache_option\":{\"on_off\":false,\"option\":\"cache\"}},\"extract_fields\":[{\"field\":\"di_string\",\"data_type\":\"STRING\",\"length\":\"10\",\"tag\":\"source_column\",\"option\":\"\"},{\"field\":\"di_int\",\"data_type\":\"NUMBER\",\"length\":\"10\",\"tag\":\"source_column\",\"option\":\"\"},{\"field\":\"di_decimal\",\"data_type\":\"DECIMAL\",\"length\":\"10,2\",\"tag\":\"source_column\",\"option\":\"\"},{\"field\":\"di_date\",\"data_type\":\"DATE\",\"length\":\"10\",\"tag\":\"source_column\",\"option\":\"\"},{\"field\":\"di_time\",\"data_type\":\"TIME\",\"length\":\"8\",\"tag\":\"source_column\",\"option\":\"\"},{\"field\":\"di_datetime\",\"data_type\":\"DATETIME\",\"length\":\"19\",\"tag\":\"source_column\",\"option\":\"\"}]}}},{\"operator_name\":\"mysql\",\"name\":\"target_1565254085099\",\"id\":\"1908081648122\",\"type\":\"sink\",\"strategy\":{\"clazz\":\"\",\"config\":{\"extender\":{\"auth\":{\"params\":{\"ds_name\":\"di_mysql_test\",\"user_id\":\"1\",\"project_id\":149,\"ds_type\":\"mysql\",\"ds_id\":100},\"clazz\":\"\"},\"meta\":{\"params\":{\"ds_name\":\"di_mysql_test\",\"user_id\":\"1\",\"project_id\":149,\"ds_type\":\"mysql\",\"ds_id\":100},\"clazz\":\"\"}},\"db_name\":\"di_mysql_check\",\"write_option\":\"append\",\"table_name\":\"di_tech_sink\",\"advanced_options\":{},\"extract_fields\":[{\"field_props\":{\"key_type\":\"\",\"not_null\":\"false\",\"is_partition\":false,\"default_value\":\"\"},\"field\":\"d_string\",\"from_field\":\"di_string\",\"data_type\":\"STRING\",\"length\":\"10\"},{\"field_props\":{\"key_type\":\"\",\"not_null\":\"false\",\"is_partition\":false,\"default_value\":\"\"},\"field\":\"d_int\",\"from_field\":\"di_int\",\"data_type\":\"NUMBER\",\"length\":\"10\"},{\"field_props\":{\"key_type\":\"\",\"not_null\":\"false\",\"is_partition\":false,\"default_value\":\"\"},\"field\":\"d_decimal\",\"from_field\":\"di_decimal\",\"data_type\":\"DECIMAL\",\"length\":\"10,2\"},{\"field_props\":{\"key_type\":\"\",\"not_null\":\"false\",\"is_partition\":false,\"default_value\":\"\"},\"field\":\"d_date\",\"from_field\":\"di_date\",\"data_type\":\"DATE\",\"length\":\"10\"},{\"field_props\":{\"key_type\":\"\",\"not_null\":\"false\",\"is_partition\":false,\"default_value\":\"\"},\"field\":\"d_time\",\"from_field\":\"di_time\",\"data_type\":\"TIME\",\"length\":\"8\"},{\"field_props\":{\"key_type\":\"\",\"not_null\":\"false\",\"is_partition\":false,\"default_value\":\"\"},\"field\":\"d_datetime\",\"from_field\":\"di_datetime\",\"data_type\":\"DATETIME\",\"length\":\"19\"}]}}}]}";
//        String syncStr = "{\"graph\": {\"edges\":[{\"destination\":{\"node\":\"1908081648122\",\"seq\":\"0\"},\"source\":{\"node\":\"1908081646036\",\"seq\":\"0\"}},{\"destination\":{\"node\":\"955737\",\"seq\":\"0\"},\"source\":{\"node\":\"1908081644133\",\"seq\":\"0\"}},{\"destination\":{\"node\":\"1908081646036\",\"seq\":\"0\"},\"source\":{\"node\":\"955737\",\"seq\":\"0\"}},{\"destination\":{\"node\":\"5955737\",\"seq\":\"0\"},\"source\":{\"node\":\"955737\",\"seq\":\"0\"}}],\"widgets\":[{\"run_env\":\"test\",\"operator_name\":\"techCheck\",\"name\":\"source_1565253881102\",\"id\":\"955737\",\"type\":\"transform\",\"strategy\":{\"clazz\":\"\",\"config\":{\"extender\":{\"check\":{\"params\":{\"ds_name\":\"di_mysql_test\",\"rule_ids\":[1,33],\"project_id\":149,\"ds_type\":\"mysql\",\"ds_id\":100},\"clazz\":\"\"}},\"db_name\":\"di_mysql_check\",\"table_name\":\"di_tech_check\",\"extract_fields\":[{\"field\":\"di_string\",\"data_type\":\"STRING\",\"length\":\"10\",\"option\":{\"tech_check\":{\"rule_options\":[{\"rule_id\":1,\"__key__\":\"01DHR6FNACS9R1YVT6P4VD6QZH\",\"rule_type\":\"MESSY_CODE\",\"rule_name\":\"乱码检查\",\"__index__\":0}],\"on_off\":true}}},{\"field\":\"di_int\",\"data_type\":\"NUMBER\",\"length\":\"10\",\"option\":{\"tech_check\":{\"rule_options\":[{\"rule_id\":33,\"__key__\":\"01DHR6EVSYW7C50VAH0P9E6YYS\",\"rule_type\":\"CONTENT_LENGTH\",\"rule_name\":\"内容长度检查\",\"__index__\":0}],\"on_off\":true}}},{\"field\":\"di_decimal\",\"data_type\":\"DECIMAL\",\"length\":\"10,2\",\"option\":{\"tech_check\":{\"rule_options\":[],\"on_off\":false}}},{\"field\":\"di_date\",\"data_type\":\"DATE\",\"length\":\"10\",\"option\":{\"tech_check\":{\"rule_options\":[],\"on_off\":false}}},{\"field\":\"di_time\",\"data_type\":\"TIME\",\"length\":\"8\",\"option\":{\"tech_check\":{\"rule_options\":[],\"on_off\":false}}},{\"field\":\"di_datetime\",\"data_type\":\"DATETIME\",\"length\":\"19\",\"option\":{\"tech_check\":{\"rule_options\":[],\"on_off\":false}}}]}}},{\"operator_name\":\"checkSink\",\"name\":\"检核上报\",\"id\":\"5955737\",\"type\":\"virtual\"},{\"operator_name\":\"mysql\",\"name\":\"source_1565253881102\",\"id\":\"1908081644133\",\"type\":\"source\",\"strategy\":{\"clazz\":\"\",\"config\":{\"filter\":\"\",\"extender\":{\"auth\":{\"params\":{\"ds_name\":\"di_mysql_test\",\"user_id\":\"1\",\"project_id\":149,\"ds_type\":\"mysql\",\"ds_id\":100},\"clazz\":\"\"},\"meta\":{\"params\":{\"ds_name\":\"di_mysql_test\",\"user_id\":\"1\",\"project_id\":149,\"ds_type\":\"mysql\",\"ds_id\":100},\"clazz\":\"\"}},\"db_name\":\"di_mysql_check\",\"table_name\":\"di_tech_check\",\"advanced_options\":{\"split_mode\":{\"split_field\":\"id\",\"on_off\":false,\"split_nums\":\"1\"}},\"extract_fields\":[{\"colName\":\"di_string\",\"colType\":\"STRING\",\"length\":\"10\",\"colCode\":\"col-b26b748935ff4805a1e64e0408e7ff0c\",\"techLogicTableId\":1592,\"id\":6214,\"position\":0},{\"colName\":\"di_int\",\"colType\":\"NUMBER\",\"length\":\"10\",\"colCode\":\"col-9964ec91585e4eeb853cda6b3604b8b3\",\"techLogicTableId\":1592,\"id\":6215,\"position\":0},{\"colName\":\"di_decimal\",\"colType\":\"DECIMAL\",\"length\":\"10,2\",\"colCode\":\"col-ad28868c43124883923499abed0c5bf2\",\"techLogicTableId\":1592,\"id\":6216,\"position\":0},{\"colName\":\"di_date\",\"colType\":\"DATE\",\"length\":\"10\",\"colCode\":\"col-f80e87015f65417281121150d3d44d54\",\"techLogicTableId\":1592,\"id\":6217,\"position\":0},{\"colName\":\"di_time\",\"colType\":\"TIME\",\"length\":\"8\",\"colCode\":\"col-ed3ee45bb2ce489a88a06fc5fa5a075a\",\"techLogicTableId\":1592,\"id\":6218,\"position\":0},{\"colName\":\"di_datetime\",\"colType\":\"DATETIME\",\"length\":\"19\",\"colCode\":\"col-57e8f2f3ae094a30a7c325228fab20d1\",\"techLogicTableId\":1592,\"id\":6219,\"position\":0}]}}},{\"operator_name\":\"map\",\"name\":\"map_1565253999011\",\"id\":\"1908081646036\",\"type\":\"transform\",\"strategy\":{\"clazz\":\"\",\"config\":{\"filter\":\"\",\"advanced_options\":{\"partition\":{\"on_off\":false,\"nums\":\"\"},\"cache_option\":{\"on_off\":false,\"option\":\"cache\"}},\"extract_fields\":[{\"field\":\"di_string\",\"data_type\":\"STRING\",\"length\":\"10\",\"tag\":\"source_column\",\"option\":\"\"},{\"field\":\"di_int\",\"data_type\":\"NUMBER\",\"length\":\"10\",\"tag\":\"source_column\",\"option\":\"\"},{\"field\":\"di_decimal\",\"data_type\":\"DECIMAL\",\"length\":\"10,2\",\"tag\":\"source_column\",\"option\":\"\"},{\"field\":\"di_date\",\"data_type\":\"DATE\",\"length\":\"10\",\"tag\":\"source_column\",\"option\":\"\"},{\"field\":\"di_time\",\"data_type\":\"TIME\",\"length\":\"8\",\"tag\":\"source_column\",\"option\":\"\"},{\"field\":\"di_datetime\",\"data_type\":\"DATETIME\",\"length\":\"19\",\"tag\":\"source_column\",\"option\":\"\"}]}}},{\"operator_name\":\"mysql\",\"name\":\"target_1565254085099\",\"id\":\"1908081648122\",\"type\":\"sink\",\"strategy\":{\"clazz\":\"\",\"config\":{\"extender\":{\"auth\":{\"params\":{\"ds_name\":\"di_mysql_test\",\"user_id\":\"1\",\"project_id\":149,\"ds_type\":\"mysql\",\"ds_id\":100},\"clazz\":\"\"},\"meta\":{\"params\":{\"ds_name\":\"di_mysql_test\",\"user_id\":\"1\",\"project_id\":149,\"ds_type\":\"mysql\",\"ds_id\":100},\"clazz\":\"\"}},\"db_name\":\"di_mysql_check\",\"write_option\":\"append\",\"table_name\":\"di_tech_sink\",\"advanced_options\":{},\"extract_fields\":[{\"field_props\":{\"key_type\":\"\",\"not_null\":\"false\",\"is_partition\":false,\"default_value\":\"\"},\"field\":\"d_string\",\"from_field\":\"di_string\",\"data_type\":\"STRING\",\"length\":\"10\"},{\"field_props\":{\"key_type\":\"\",\"not_null\":\"false\",\"is_partition\":false,\"default_value\":\"\"},\"field\":\"d_int\",\"from_field\":\"di_int\",\"data_type\":\"NUMBER\",\"length\":\"10\"},{\"field_props\":{\"key_type\":\"\",\"not_null\":\"false\",\"is_partition\":false,\"default_value\":\"\"},\"field\":\"d_decimal\",\"from_field\":\"di_decimal\",\"data_type\":\"DECIMAL\",\"length\":\"10,2\"},{\"field_props\":{\"key_type\":\"\",\"not_null\":\"false\",\"is_partition\":false,\"default_value\":\"\"},\"field\":\"d_date\",\"from_field\":\"di_date\",\"data_type\":\"DATE\",\"length\":\"10\"},{\"field_props\":{\"key_type\":\"\",\"not_null\":\"false\",\"is_partition\":false,\"default_value\":\"\"},\"field\":\"d_time\",\"from_field\":\"di_time\",\"data_type\":\"TIME\",\"length\":\"8\"},{\"field_props\":{\"key_type\":\"\",\"not_null\":\"false\",\"is_partition\":false,\"default_value\":\"\"},\"field\":\"d_datetime\",\"from_field\":\"di_datetime\",\"data_type\":\"DATETIME\",\"length\":\"19\"}]}}}]}}";
//        Map<String, String> map = null;
//        try {
//            map = parseSyncStr(syncStr);
//        } catch (ParamParseException e) {
//            e.printStackTrace();
//        }
//        System.out.println(JSONObject.toJSONString(map).replaceAll("\"", "\\\\\""));

        Props props = new Props();
        props.put("plugin_param", "{\"run_env\":\"test\",\"operator_name\":\"mpp\",\"name\":\"mppsql_error\",\"id\":\"1908101500623\",\"type\":\"sql\",\"strategy\":{\"config\":{\"extender\":{\"auth\":{\"params\":{\"ds_type\":\"mpp\",\"ds_id\":215},\"clazz\":\"\"},\"meta\":{\"params\":{\"ds_type\":\"mpp\",\"ds_id\":215},\"clazz\":\"\"}},\"advanced_options\":{},\"sql\":\"insert into di_mpp_db.src_table_cur\\r\\nselect * from di_mpp_db.src_table;\"},\"clazz\":\"\"},\"clazz\":\"\"}");

        String pluginParams = "{\"plugin_param\":{\"run_env\":\"test\",\"operator_name\":\"mpp\",\"name\":\"mpp_sql\",\"id\":\"1908161639963\",\"type\":\"sql\",\"strategy\":{\"config\":{\"extender\":{\"auth\":{\"params\":{\"ds_type\":\"mpp\",\"ds_id\":215},\"clazz\":\"\"},\"meta\":{\"params\":{\"ds_type\":\"mpp\",\"ds_id\":215},\"clazz\":\"\"}},\"advanced_options\":{},\"sql\":\"insert into di_mpp_db.del_table\\r\\nselect * from di_mpp_db.src_table;\"},\"clazz\":\"\"},\"clazz\":\"\"}}";
        DsInfo dsInfo = extractDsInfo(props.get("plugin_param"));
        //        Map<String, Object> sMap = new HashMap<>();
//        for (String key : props.getKeySet()) {
//            sMap.put(key, props.get(key));
//        }
//        Map code = parseSyncMap(sMap);
//        String json = JSON.toJSONString(JSON.toJSONString(code));
//        System.out.println(json);
    }
}
